﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 菜单数据列仓储接口实现
    /// </summary>
    public class SysMenuTableColsRepository : Repository<SysMenuTableCols>, ISysMenuTableColsRepository
    {
        /// <summary>
        /// 根据菜单id和字段名称获取数据列
        /// </summary>
        /// <param name="menuId">菜单id</param>
        /// <param name="fieldName">字段名称</param>
        /// <returns>返回数据列</returns>
        public SysMenuTableCols? GetTableColsByMenuIdOrFieldName(string menuId, string fieldEnName)
        {
            string sql = @" select  * from  Sys_MenuTableCols where  MenuId =@MenuId and FieldEnName=@FieldEnName ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.QueryFirstOrDefault<SysMenuTableCols>(sql, new { MenuId = menuId, FieldEnName = fieldEnName });
        }

        /// <summary>
        /// 根据菜单id获取列
        /// </summary>
        /// <param name="menuId">菜单ID</param>
        /// <returns></returns>
        public List<SysMenuTableColsOutPut> GetTableColsByMenuId(string menuId)
        {
            string sql = @" select  sd.RuleJson,* from  Sys_MenuTableCols as sc
                            left join Sys_DataRule as sd on sc.RuleId = sd.RuleId and sd.IsOpen=1
                             where  sc.MenuId =@MenuId
                             and IsDisplay= 1  order by FieldOrderBy";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysMenuTableColsOutPut>(sql, new { MenuId = menuId }).ToList();
        }

        /// <summary>
        /// 修改字段信息
        /// </summary>
        /// <param name="sysMenuTableCols"></param>
        public void UpdateTableCols(SysMenuTableCols sysMenuTableCols)
        {
            string sql = @" update  Sys_MenuTableCols set  FieldCnName = @FieldCnName,FieldEnName=@FieldEnName,FieldType=@FieldType  where  FieldId = @FieldId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { sysMenuTableCols.FieldId, sysMenuTableCols.FieldCnName, sysMenuTableCols.FieldEnName, sysMenuTableCols.FieldType });
        }

        /// <summary>
        /// 更新字段规则ID
        /// </summary>
        /// <param name="fieldId">字段ID</param>
        /// <param name="ruleId">规则ID</param>
        public void UpdateRuleId(string fieldId, string ruleId)
        {
            string sql = @" update  Sys_MenuTableCols set  RuleId = @RuleId  where  FieldId = @FieldId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Execute(sql, new { FieldId = fieldId, RuleId = ruleId });
        }

        /// <summary>
        /// 根据菜单id，获取数据列
        /// </summary>
        /// <param name="menuId">菜单id</param>
        /// <returns>返回对应列集合</returns>
        public List<SysMenuTableCols> GetTableCols(string menuId)
        {
            string sql = @" select  FieldCnName,FieldEnName,FieldType  from  Sys_MenuTableCols 
                            where MenuId = @MenuId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysMenuTableCols>(sql, new { MenuId = menuId }).ToList();
        }
    }
}
